<?php
/**
 * 促销规则
 * @author 李先生
 *
 */
namespace Org\Oa;
use Think\Controller;
class promotion extends Controller{
	/**
	 * 根据产品的id来查询促销的价格
	 * $type 1 获取规则 2是获取促销后价格
	 * $method 0 是商品促销 1是订单促销
	 *  当为商品促销时 goods_id为产品的id  为订单促销时 goods_id 为订单金额
	 */
	public static function product_for_promotion($uid,$goods_id,$type,$method){
		$now = date("Y-m-d H:i:s");
		if($method == 1){
			$promolist = M('promotion')->where("conditions <= '{$goods_id}'  and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='{$method}'")->order("id desc")->find();
		}else{
			$promolist = M('promotion')->where("find_in_set('{$goods_id}',goods_id) and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='{$method}'")->order("id desc")->find();
		}
		
		if(!empty($promolist)){
			if($promolist['user_group'] !='all'){
				$group_id = M('user')->where("id='{$uid}'")->getField('group_id');
				$user_group = explode(",",$promolist['user_group']);
				if(!in_array($goods_id, $user_group)){
					$promolist = "";
				}
			}
		}
		if($type == 1){
			//获取优惠规则
			if(empty($promolist)){
				return "";exit;
			}else{
				$rule = self::promotion_role($promolist['type'], $promolist['award_type'], $promolist['award_value']);
				return $rule;exit();
			}
			
		}else{
			//获取优惠后的价格
			if($method == 0){
				$goods_price = M('products')->where("id='{$goods_id}'")->field('goods_id,sell_price')->find();
				$group_price = self::group_price($uid, $goods_price['goods_id'], $goods_id);
				if($group_price){
					$newprice = $group_price;
				}else{
					$newprice = $goods_price['sell_price'];
				}
				if(empty($promolist)){
					return $newprice;exit();
				}else{
					$rule = self::promotion_over_price($newprice,$promolist['type'], $promolist['award_type'], $promolist['award_value']);
					return $rule;exit();
				}
			}else{
				$rule = self::promotion_over_price($goods_id,$promolist['type'], $promolist['award_type'], $promolist['award_value']);
				return $rule;exit();
			}
		}
	}
	/**
	 * 获取促销规则
	 */
	public static function promotion_role($type,$award_type,$award_value){
		if($type == 0){
			//商品促销
			switch ($award_type){
				case '1':
					$name = "现在购买此商品，打".($award_value/10)."折";
				break;
				case '2':
					$name = "现在购买此商品，立减".$award_value."元";
				break;
				case '3':
					$name = "现在购买此商品，价格为".$award_value."元";
				break;
				case '4':
					$ticket = M('ticket')->where("id='{$award_value}'")->getField('name');
					$name = "现在购买此商品，就送一张".$ticket;
				break;
			}
		}else{
		    //订单促销
			switch ($award_type){
				case '1':
					$name = "打".($award_value/10)."折";
					break;
				case '2':
					$name = "立减".$award_value."元";
					break;
				case '3':
					$name = "增送".$award_value."积分";
					break;
				case '4':
					$ticket = M('ticket')->where("id='{$award_value}'")->getField('name');
					$name = "送一张".$ticket;
				break;
				case '5':
					$name = "满".$award_value."元，免运费";
				break;
			}	
		}
		return $name;
	}
	/**
	 * 计算促销后的价格
	 */
	public static function promotion_over_price($goods_price,$type,$award_type,$award_value){
		if($type == 0){
			//商品促销
			switch ($award_type){
				case '1':
					$price = $goods_price*($award_value/100);
				break;
				case '2':
					$price = $goods_price-$award_value;
				break;
				case '3':
					$price = $award_value;
				break;
				default:
					$price = $goods_price;
				break;
			}
		}else{
			//订单促销
			switch ($award_type){
				case '1':
					$price = $goods_price*($award_value/100);
				break;
				case '2':
					$price = $goods_price-$award_value;
				break;
				default:
					$price = $goods_price;
				break;
			}
		}
		return $price;
	}
	/**
	 * 是否满足会员组价格
	 */
	public static function group_price($uid,$goods_id,$product_id){
		 $group_id = M('user')->where("id='{$uid}'")->getField('group_id');
		 if($group_id){
		 	$price = M('group_price')->where("goods_id='{$goods_id}' and product_id='{$product_id}' and group_id='{$group_id}'")->getField('price');
		 	return $price;
		 }else{
		 	return 0;
		 }
	}
	/**
	 * 是否满足送优惠券
	 * $method 0 是商品促销 1是订单促销
	 * 当为商品促销时 goods_id为产品的id  为订单促销时 goods_id 为订单金额
	 */
	public static function prefer_for_promotion($uid,$goods_id,$method){
		$now = date("Y-m-d H:i:s");
		if($method == 1){
			$promolist = M('promotion')->where("conditions <= '{$goods_id}'  and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='{$method}' and award_type = '4'")->order("id desc")->find();
		}else{
			$promolist = M('promotion')->where("find_in_set('{$goods_id}',goods_id) and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='{$method}' and award_type = '4'")->order("id desc")->find();
		}
		if(!empty($promolist)){
			if($promolist['user_group'] !='all'){
				$group_id = M('user')->where("id='{$uid}'")->getField('group_id');
				$user_group = explode(",",$promolist['user_group']);
				if(!in_array($goods_id, $user_group)){
					$promolist = "";
				}
			}
		}
		if(!empty($promolist)){
			//$ma = \Org\Oa\prefer::add_code($promolist['award_value'], 1);
			$data['ticket_id'] = $promolist['award_value'];
			$data['card_num'] = \Org\Oa\prefer::parekey('prop','card_num',10);
			$data['is_use'] = 0;
			$data['is_send'] = 0;
			$data['send_time'] = date("Y-m-d H:i:s");
			$data['uid'] = $uid;
			$id = M('prop')->add($data);
			M('ticket')->where("id='{$promolist['award_value']}'")->setInc('send_num',1);
			return $id;
		}else{
			return "";
		}
	}
	/**
	 * 是否满足增加积分
	 */
	public static function point_for_promotion($uid,$goods_id,$method){
		$now = date("Y-m-d H:i:s");
		//if($method == 1){
		$promolist = M('promotion')->where("conditions <= '{$goods_id}'  and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='1' and award_type = '3'")->order("id desc")->find();
		//}else{
			//$promolist = M('promotion')->where("find_in_set('{$goods_id}',goods_id) and status='0' and start_time <= '{$now}' and end_time >= '{$now}' and type ='{$method}' and award_type = '4'")->order("id desc")->find();
		//}
		if(!empty($promolist)){
			if($promolist['user_group'] !='all'){
				$group_id = M('user')->where("id='{$uid}'")->getField('group_id');
				$user_group = explode(",",$promolist['user_group']);
				if(!in_array($goods_id, $user_group)){
					$promolist = "";
				}
			}
		}
		if(!empty($promolist)){
			return $promolist['award_value'];
		}else{
			return 0;
		}
	}
}